package com.app.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.app.mapper.UserMapper;
import com.app.pojo.User;
import com.app.service.IGasDetailsService;
import com.app.util.Result;
import com.app.vo.GasDetailsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 燃气详细信息 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2024-07-29
 */
@Api(tags = "燃气表信息相关接口")
@RestController
@RequestMapping("/gasDetails")
public class GasDetailsController {

    @Autowired
    private IGasDetailsService gasDetailsService;

    @Autowired
    private UserMapper userMapper;


    @ApiOperation(value = "根据公司名称和账户查询余额和基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = true, dataType = "String"),
            @ApiImplicitParam(name = "companyName", value = "公司名称", required = true, dataType = "String")

    })
    @GetMapping("/getGasDetailsInfo")
    public Result getGasDetailsInfo(String account, String companyName, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error( "token不存在");
        }
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken == null) {
            Result result = new Result(401, "登录失效");
            return result;
        }
        String userId = loginIdByToken.toString();
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error( "用户不存在");
        }
        if(!StringUtils.isNotEmpty(companyName) || !StringUtils.isNotEmpty(account)){
            return Result.error("请输入缴费单位名称和账户号！");
        }
        GasDetailsVo gasDetailsVo = gasDetailsService.getGasDetailsInfo(account, companyName);
        if (gasDetailsVo == null) {
            return Result.error("未查询到账户信息！请检查户号和公司名称是否正确！");
        }
        return Result.success(gasDetailsVo);
    }



    @ApiOperation(value = "查询当前登陆人的历史缴费账户信息数组")
    @GetMapping("/getGasDetailsInfoList")
    public Result getGasDetailsInfoList(Integer page,Integer limit, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error( "token不存在");
        }
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken == null) {
            Result result = new Result(401, "登录失效");
            return result;
        }
        String userId = loginIdByToken.toString();
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error( "用户不存在");
        }
        GasDetailsVo list = gasDetailsService.getMyGasDetailsInfoList(page,limit,Integer.valueOf(userId));
        return Result.success(list);
    }


    @ApiOperation(value = "根据户号查询缴费总额和总使用燃气量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账户", required = true, dataType = "String")
    })
    @GetMapping("/getGasTotalInfo")
    public Result getGasTotalInfo(@RequestParam(required = false) String account, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error( "token不存在");
        }
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken == null) {
            Result result = new Result(401, "登录失效");
            return result;
        }
        String userId = loginIdByToken.toString();
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error( "用户不存在");
        }
//        if(!StringUtils.isNotEmpty(account)){
//            return Result.error("请输入账户号！");
//        }
//        if (StringUtils.isEmpty(account)) {
//            Map<String, Object> map = new HashMap<>();
//            map.put("sumMoney", 0.0);
//            map.put("gas_meter_total", 0);
//            return Result.success(map);
//        }
        Map<String, Object> map = gasDetailsService.getSumGasDetailsInfo(account,user.getId());
        if (map.isEmpty()) {
            return Result.error("未查询到账户信息！请检查户号是否正确！");
        }
        return Result.success(map);
    }


}

